package com.easyrecite.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.easyrecite.dao.UserDao;
import com.easyrecite.activity.R;
import com.easyrecite.utils.PhoneCheckUtil;

import java.sql.SQLException;

public class LoginMainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText phone;
    private EditText password;
    private Button btn_login;
    private Button btn_signup;
    private Button btn_forgetPassword;
    private ImageButton btn_back;
    private TextView phone_check;
    private TextView password_check;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        setContentView(R.layout.activity_login_main);
        init();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String Phone = phone.getText().toString();
                String Password = password.getText().toString();
                boolean flag1, flag2;
                if (Phone.equals("") || !PhoneCheckUtil.isPhoneLegal(Phone)) {
                    flag1 = false;
                    phone_check.setText("手机号格式有误");
                } else {
                    flag1 = true;
                    phone_check.setText("");
                }
                if (Password.equals("")) {
                    flag2 = false;
                    password_check.setText("密码格式有误");
                } else {
                    flag2 = true;
                    password_check.setText("");
                }
                if (flag1 && flag2) {
                    // TODO: 2021/12/14
                    //  Database operation(username, phone, password)
                    boolean tmp = false;
                    UserDao userDao = new UserDao();
                    try {
                        if (android.os.Build.VERSION.SDK_INT > 9) {
                            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                            StrictMode.setThreadPolicy(policy);
                        }
                        tmp = userDao.login(Phone,Password);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    if (tmp){
                        Toast.makeText(this, "登陆成功，欢迎你！", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(this, "用户名或密码错误!", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.btn_forget_password:
                Toast.makeText(this, "功能开发中..", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_signup:
                Intent intent = new Intent(this, SignupMainActivity.class);
                startActivityForResult(intent, 100);
                break;
            case R.id.login_back:
                finish();
                break;
        }
    }

    private void init() {
        phone = (EditText) findViewById(R.id.phone);
        password = (EditText) findViewById(R.id.password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_signup = (Button) findViewById(R.id.btn_signup);
        btn_forgetPassword = (Button) findViewById(R.id.btn_forget_password);
        btn_login.setOnClickListener(this);
        btn_signup.setOnClickListener(this);
        btn_forgetPassword.setOnClickListener(this);
        sharedPreferences = getSharedPreferences("Info", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        phone_check = findViewById(R.id.phone_check);
        password_check = findViewById(R.id.password_check);
        btn_back = (ImageButton) findViewById(R.id.login_back);
        btn_back.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 100) {
            Bundle bundle = data.getExtras();
            String Phone = bundle.getString("phone");
            String Password = bundle.getString("password");
            phone.setText(Phone);
            password.setText(Password);
        }
    }
}